package br.com.Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.Conexao.conexao;
import br.com.Modelo.Endereco;
import br.com.Modelo.Produto;
import br.com.Modelo.UsuarioCliente;
import br.com.Modelo.Pagina;
import br.com.Modelo.UsuarioComerciante;

public class PaginaDao {
	
	Connection connection;
	
	public PaginaDao(){
		
		this.connection = new conexao().getConnection();
		
	}
	
	
	  public void adiciona(Pagina pagina) {
	    	
	    	//if(isLoginEqual(usuario)==false){
		    String sql = "insert into tb_pagina " +
		            "(pagina,imgCapa)" +
		            " values (?,?)";

		    try {
		        // prepared statement para inserção
		        PreparedStatement stmt = (PreparedStatement) connection.prepareStatement(sql);

		        // seta os valores

		        stmt.setString(1,pagina.getPagina());
		        stmt.setString(2,pagina.getImgPagina());
		  
		   

		        // executa
		        stmt.execute();
		        System.out.print("Pagina add com sucesso");
		        stmt.close();
		    } catch (SQLException e) {
		        throw new RuntimeException(e);
		    }
	    
		}
	  

	  public void addCapa(String img , int idpagina) {
	    	
	    	//if(isLoginEqual(usuario)==false){
		    String sql = "update tb_pagina set imgCapa=? where id_pagina='"+idpagina+"'";
		           
		

		    try {
		        // prepared statement para inserção
		        PreparedStatement stmt = (PreparedStatement) connection.prepareStatement(sql);

		        // seta os valores

		        stmt.setString(1,img);
		       
		  
		   

		        // executa
		        stmt.execute();
		        System.out.print("Pagina add com sucesso");
		        stmt.close();
		    } catch (SQLException e) {
		        throw new RuntimeException(e);
		    }
	    
		}
	  
	  //select * from #teste where NUM_VENDA in ( select MAX(num_venda) from #teste group by CODIGO)
	  
	
	    
	  public int getFirstIdPage(){
		  
		  String sql = "SELECT id_pagina id FROM tb_pagina WHERE id_pagina IN (SELECT MAX(id_pagina) FROM tb_pagina)";
		  
		  		try {	
				
				PreparedStatement stmt = (PreparedStatement) this.connection.prepareStatement(sql);
				ResultSet rs= stmt.executeQuery(); 
				if(rs.next()){
	                	 
	                	return rs.getInt("id");
	                	 
	            }		
			    	 
				
					stmt.close();
					//connection.close();
				  
				 } catch (SQLException e) {
				         throw new RuntimeException(e);
				  }
	    
		  
		  
		  return -1;
	  }
	  
	  public Pagina getPagina(long id_pagina) {
			try {	
			

				
			PreparedStatement stmt = (PreparedStatement) this.connection.prepareStatement("select * from tb_pagina where id_pagina='"+id_pagina+"'");
			ResultSet rs= stmt.executeQuery();
			
				while(rs.next()){
				    Pagina pagina = new Pagina();
				    pagina.setId_pagina(rs.getLong("id_pagina"));
				    pagina.setPagina(rs.getString("pagina"));
				    pagina.setImgPagina(rs.getString("imgCapa"));
				    return pagina;
				    
			    
			     }
				rs.close();
				stmt.close();
				connection.close();
			     
			 } catch (SQLException e) {
			         throw new RuntimeException(e);
			  }
			  return null;
		    }
	  
	  
	    public boolean isPaginaEqual(String pagina) {
			try {	
              System.out.println("validando "+pagina);
			PreparedStatement stmt = (PreparedStatement) this.connection.prepareStatement("select pagina from tb_pagina where pagina='"+pagina+"'");
			ResultSet rs= stmt.executeQuery();
		  // System.out.print(rs.getFetchSize());
				if(rs.next()){		
				   		  
			          return true;			
			      }
				rs.close();
				stmt.close();
				//connection.close();
			     return false;
			 } catch (SQLException e) {
			         throw new RuntimeException(e);
			  }
		    }
	  
public Pagina getPaginaComplet(String perfil_pagina) {
			try {	
			
				UsuarioComerciante comerciante= new UsuarioComerciante();
 			    Pagina pagina = new Pagina();
 			    int aux=0;
 			    int cont=0;
 			    int c=0;
 			    
				ProdutoDao dao = new ProdutoDao();
				EnderecoDao daoEnd = new EnderecoDao();
			
				
 			    String sql = "SELECT * FROM tb_pagina a , tb_usuario_comerciante b"+
                              " WHERE a.id_pagina = b.idPagina"+                           
                              " AND a.pagina='"+perfil_pagina+"'";
                             
				 
			PreparedStatement stmt = (PreparedStatement) this.connection.prepareStatement(sql);
			ResultSet rs= stmt.executeQuery();
			
				while(rs.next()){
				    
		
						
								pagina.setId_pagina(rs.getLong("id_pagina"));
								pagina.setPagina(rs.getString("pagina"));
								pagina.setImgPagina(rs.getString("imgCapa"));
							    comerciante.setId(rs.getLong("idUsuarioComerciante"));
							    comerciante.setNome(rs.getString("nome"));
							    comerciante.setLogin(rs.getString("login"));
							    comerciante.setSenha(rs.getString("senha"));
							    comerciante.setRamo(rs.getString("ramo"));
							    comerciante.setCnpj(rs.getString("cnpj"));
							    comerciante.setAutenticacao(Integer.parseInt(rs.getString("autenticacao")));			 
							    comerciante.setIdPagina(rs.getLong("idPagina"));
							  
							    
					
				        
				
				}
				pagina.setUsuarioComerciante(comerciante);
				pagina.setEnds(daoEnd.getbyEnd(perfil_pagina));
				pagina.setProduto(dao.getProdbyPagina(perfil_pagina));			  
				rs.close();
				stmt.close();				
				connection.close();
				return pagina;
			     
			 } catch (SQLException e) {
			         throw new RuntimeException(e);
			  }
		
		    }


	  
}
